#include "my_tcpserver.h"
#include <QtNetwork>
#include <QString>

my_TCPServer::my_TCPServer()
{
    tcpServer=new QTcpServer;
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
}

bool my_TCPServer::open(QString IP, int port)
{


    return tcpServer->listen(QHostAddress(IP),port);
}

int my_TCPServer::send(QString buf, int id)
{
    my_Socket *m_socket;
    m_socket = Socketlist.value(id);
    m_socket->send(buf);
}

void my_TCPServer::close()
{
    tcpServer->close();
}

void my_TCPServer::onNewConnection()
{
    order++;
    count++;
    my_Socket *m_socket;
    m_socket=new my_Socket(tcpServer->nextPendingConnection(),order);
    Socketlist.insert(order,m_socket);
    emit connected(order);
    connect(m_socket,SIGNAL(readyRead(int)),
            this,SLOT(onSocketReadyRead(int)));
    connect(m_socket,SIGNAL(disconnected(int)),
            this,SLOT(onClientDisconnected(int)));


}

void my_TCPServer::onSocketReadyRead(int id)
{
    my_Socket *m_socket;
    m_socket = Socketlist.value(id);
    QString buf;
    while(m_socket->my_tcpSocket->canReadLine())
        buf = m_socket->my_tcpSocket->readLine();
    readresult(buf,id);
    execute(buf,id);
}

void my_TCPServer::onClientDisconnected(int id)
{

    Socketlist.remove(id);
    count--;
    emit disconnected(id);
}

void my_TCPServer::execute(QString buf,int id)
{
//    QStringList  strs=  buf.split(":");
//    if(strs.size()==0)
//        return;
//    if(strs.at(0).contains("relay",Qt::CaseInsensitive))
//    {
//        QStringList  strs2=  strs.at(1).split(" ");
//        if(strs2.size()==2)
//        {
//            int chanl= strs2.at(0).right(1).toInt();
//            int num=strs2.at(1).toInt();
//            relay_control(1,chanl,num);
//        }
//        else if(strs2.size()==1)
//        {
//            if(strs2.at(0).contains("ch",Qt::CaseInsensitive)&&strs2.at(0).contains("?"))
//            {
//                QString ch;
//                switch (strs2.at(0).left(3).right(1).toInt())
//                {
//                case 1:ch=QString::number(indexIO1);break;
//                default:ch="error";
//                }
//                send(ch,id);
//            }
//        }
//    }
}

my_Socket::my_Socket(QTcpSocket *tcpSocket,int order)
{
    my_tcpSocket = tcpSocket ; //创建socket
    id=order;
    connect(my_tcpSocket, SIGNAL(connected()),
            this, SLOT(onClientConnected()));
    onClientConnected();//

    connect(my_tcpSocket, SIGNAL(disconnected()),
            this, SLOT(onClientDisconnected()));

//    connect(my_tcpSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),
//            this,SLOT(onSocketStateChange(QAbstractSocket::SocketState)));
//    onSocketStateChange(tcpSocket->state());

    connect(my_tcpSocket,SIGNAL(readyRead()),
            this,SLOT(onSocketReadyRead()));
}

int my_Socket::send(QString buf)
{
    buf.append('\n');//添加一个换行符
    my_tcpSocket->write(buf.toUtf8());
}

void my_Socket::onSocketStateChange(QAbstractSocket::SocketState socketState)
{

}

void my_Socket::onClientConnected()
{
    netattr.IP = my_tcpSocket->peerAddress().toString();
    netattr.port = my_tcpSocket->peerPort();
}

void my_Socket::onClientDisconnected()
{
    my_tcpSocket->deleteLater();
    emit disconnected(id);
}

void my_Socket::onSocketReadyRead()
{
    emit readyRead(id);
//    QString buf;
//    while(my_tcpSocket->canReadLine())
//        buf = my_tcpSocket->readLine();

}
